package com.siao.net.services
{
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * Http请求服务
	 * @author SiaoLeon
	 */
	public class HttpService
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		/**
		 * 发起一个请求
		 * @param request 请求对象
		 * @param completeHandler 请求完成的回调函数（传入请求结果数据）
		 * @param errorHandler 请求错误的回调函数（传入错误事件）
		 * @param progressHandler 请求进度的回调函数（传入进度事件）
		 */
		public static function REQUEST(request:URLRequest, completeHandler:Function=null, errorHandler:Function=null, progressHandler:Function=null):void
		{
			new HttpService(completeHandler, errorHandler, progressHandler).call(request);
		}
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var loader:URLLoader;
		
		private var callbackComplete:Function;
		
		private var callbackError:Function;
		
		private var callbackProgress:Function;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 * @param completeHandler 请求完成的回调函数（传入请求结果数据）
		 * @param errorHandler 请求错误的回调函数（传入错误事件）
		 * @param progressHandler 请求进度的回调函数（传入进度事件）
		 */
		public function HttpService(completeHandler:Function=null, errorHandler:Function=null, progressHandler:Function=null)
		{
			this.callbackComplete = completeHandler;
			this.callbackError = errorHandler;
			this.callbackProgress = progressHandler;
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 发送请求
		 * @param request 请求对象
		 */
		public function call(request:URLRequest):void
		{
			this.loader = new URLLoader();
			this.loader.addEventListener(Event.COMPLETE, onComplete);
			this.loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
			this.loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			this.loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			this.loader.load(request);
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		/**
		 * 请求完成时
		 * @param event
		 */
		private function onComplete(event:Event):void
		{
			var data:* = this.loader.data;
			
			if (this.callbackComplete != null)
			{
				this.callbackComplete(data);
			}
		}
		
		/**
		 * 请求进度时
		 * @param event
		 */
		private function onProgress(event:ProgressEvent):void
		{
			if (this.callbackProgress != null)
			{
				this.callbackProgress(event);
			}
		}
		
		/**
		 * 请求错误时
		 * @param event
		 */
		private function onError(event:ErrorEvent):void
		{
			if (this.callbackError != null)
			{
				this.callbackError(event);
			}
		}
	
	}
}
